﻿using System.Windows.Forms;
using oqis.gis.frame;
using System.Collections.Generic;

namespace oqis.gis.frame.oqgSystem
{
    public class SysControl
    {
        #region 类成员变量

        private IOQGApplication _oqgApplication;

        #endregion

        #region 类构建与初始化

        public SysControl(IOQGApplication oqgApplication) { _oqgApplication = oqgApplication; }

        #endregion

        #region 类方法

   /*     
        /// <summary>
        /// 绑定Command到控件上
        /// </summary>
        /// <param name="command">命令</param>
        /// <param name="barButton">按钮</param>
        /// <param name="mapControl">地图控件</param>
        public void BindCommandItem(IBaseCommand command, BarButtonItem barButton)
        {
            // 判断
            if (command == null || barButton == null) return;
            // 显示的命令名称
            barButton.Caption = command.CommandText;
            // 图片
            barButton.Glyph = command.CommandImage;

            #region 设置提示

            DevExpress.Utils.SuperToolTip toolTip = new DevExpress.Utils.SuperToolTip();
            DevExpress.Utils.ToolTipTitleItem toolTipTitle = new DevExpress.Utils.ToolTipTitleItem();
            DevExpress.Utils.ToolTipItem tooTipContent = new DevExpress.Utils.ToolTipItem();

            toolTipTitle.Appearance.Image = command.CommandImage;
            toolTipTitle.Appearance.Options.UseImage = true;
            toolTipTitle.Image = command.CommandImage;
            toolTipTitle.Text = command.CommandText;
            tooTipContent.LeftIndent = 6;
            tooTipContent.Text = command.CommandToolTip;
            toolTip.Items.Add(toolTipTitle);
            toolTip.Items.Add(tooTipContent);
            barButton.SuperTip = toolTip;

            #endregion

            // 命令对象
            barButton.Tag = command;

            // 热键
            if ((command.ShortCut != string.Empty) && command.ShortCut != null)
            {
                KeysConverter conv = new KeysConverter();
                Keys key = (Keys)conv.ConvertFromString(command.ShortCut);
                barButton.ItemShortcut = new BarShortcut(key);
            }
            // 描述
            barButton.Description = command.CommandToolTip;

            // 可用
            barButton.Enabled = command.Enable;

            // 可见
            barButton.Visibility = command.Visible;

            // 应用程序
            command.OQGApplication = _oqgApplication;

            // 绑定事件
            barButton.ItemClick += new ItemClickEventHandler(barButton_ItemClick);
        }

        /// <summary>
        /// 绑定Command到控件上
        /// </summary>
        /// <param name="command">命令</param>
        /// <param name="barButton">按钮</param>
        /// <param name="mapControl">地图控件</param>
        public void BindCommandItem(IBaseCommand command, System.Windows.Forms.ToolStripMenuItem contextMenu)
        {
            // 判断
            if (command == null || contextMenu == null) return;
            // 显示的命令名称
            contextMenu.Text = command.CommandText;
            // 图片
            contextMenu.Image  = command.CommandImage;

            contextMenu.Tag = command;
            // 可用
            contextMenu.Enabled = command.Enable;
            
            //switch (command.Visible)
            //{
            //    case BarItemVisibility.Always:
            //        contextMenu.Visible = true;
            //        break;
            //    case BarItemVisibility.Never:
            //    case BarItemVisibility.OnlyInCustomizing:
            //    case BarItemVisibility.OnlyInRuntime:
            //        contextMenu.Visible = false;
            //        break;
            //}

            // 可见
            contextMenu.Visible = System.Convert.ToBoolean((int)command.Visible - 1);

             // 应用程序
            command.OQGApplication = _oqgApplication;

            // 绑定点击事件
            contextMenu.Click += new System.EventHandler(contextMenu_Click);
        }
        
        /// <summary>
        /// 显示可停靠用户控件
        /// </summary>
        /// <param name="dockManager">停靠控件管理器</param>
        /// <param name="panelName">面板名称</param>
        /// <param name="userControl">用户控件</param>
        /// <param name="dockStyle">停靠样式</param>
        /// <remarks>停靠只支持右依靠、下停靠和Float三种</remarks>
        public void CreateDockUserControl(string panelName, controls.QGUCBase userControl, DockingStyle dockStyle)
        {
            // 判断
            if (_oqgApplication == null || userControl == null || _oqgApplication.DockManager == null || _oqgApplication.MainForm == null) return;
            IList<DevExpress.XtraBars.Docking.DockPanel> rightList = new List<DevExpress.XtraBars.Docking.DockPanel>();
            IList<DevExpress.XtraBars.Docking.DockPanel> bottomList = new List<DevExpress.XtraBars.Docking.DockPanel>();
            IList<DevExpress.XtraBars.Docking.DockPanel> floatList = new List<DevExpress.XtraBars.Docking.DockPanel>();

            // 查找指定名称的控件，如果没有则创建
            DevExpress.XtraBars.Docking.DockManager dockManager = _oqgApplication.DockManager;

            // 是否添加新面板
            foreach (DockPanel panel in dockManager.Panels)
            {
                switch (panel.Dock)
                {
                    case DockingStyle.Top:
                    case DockingStyle.Bottom:
                        bottomList.Add(panel);
                        break;
                    case DockingStyle.Right:
                    case DockingStyle.Left:
                        rightList.Add(panel);
                        break;
                    case DockingStyle.Float:
                        floatList.Add(panel);
                        break;
                }

                // 判断是否存在
                if (panel.Name == panelName)
                {
                    panel.Visibility = DockVisibility.Visible;
                    panel.Height = 150;
                    panel.Width = 250;
                    //panel.FloatVertical = true;
                    //sameDock = (panel.Dock == dockStyle);

                    // 设置为活动面板
                    dockManager.ActivePanel = panel;

                    return;
                }
            }

            // 创建基于DockManager的Panel
            DevExpress.XtraBars.Docking.DockPanel dockPanel = dockManager.AddPanel(new System.Drawing.Point(0, 0));
            dockPanel.FloatVertical = true;
            dockPanel.Name = panelName;
            dockPanel.Text = panelName;
            dockPanel.Options.AllowDockLeft = false;
            dockPanel.Options.ShowCloseButton = false;
            dockPanel.Height = 150;
            dockPanel.Width = 250;
            dockPanel.Options.ShowMaximizeButton = false;
            dockPanel.ClosingPanel += new DockPanelCancelEventHandler(dockPanel_ClosingPanel);
            // 停靠位置
            dockPanel.Dock = dockStyle;

             // 设置停靠
            switch (dockStyle)
            {
                case DockingStyle.Top:
                case DockingStyle.Bottom:
                    if (bottomList.Count > 0) dockPanel.DockAsTab(bottomList[0]);
                    break;
                case DockingStyle.Right:
                case DockingStyle.Left:
                    if (rightList.Count > 1) dockPanel.DockAsTab(rightList[1]);
                    break;
                case DockingStyle.Float:
                    if (floatList.Count > 0) dockPanel.DockAsTab(floatList[0]);
                    break;
            }

            // 将Panel添加到DockManager
            dockManager.RootPanels.AddRange(new DevExpress.XtraBars.Docking.DockPanel[] { dockPanel });
            // 设置新加的面板为活动面板
            dockManager.ActivePanel = dockPanel;

            // 添加控件到Panel上
            dockPanel.Controls.Add(userControl);
            userControl.Dock = DockStyle.Fill;
        }

        /// <summary>
        /// 关闭Panel时，隐藏当前Panel
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void dockPanel_ClosingPanel(object sender, DockPanelCancelEventArgs e)
        {
            DevExpress.XtraBars.Docking.DockPanel dp = (DevExpress.XtraBars.Docking.DockPanel)sender;
            dp.Visibility = DockVisibility.Hidden;
            e.Cancel = true;
        }
*/
        #endregion

        #region 绑定事件
/*
        /// <summary>
        /// 点击
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void barButton_ItemClick(object sender, ItemClickEventArgs e)
        {
            if (e.Item.Tag == null) return;
            ((BaseCommand)e.Item.Tag).OnClick();
        }
*/
        /// <summary>
        /// 右键菜单点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void contextMenu_Click(object sender, System.EventArgs e)
        {
            System.Windows.Forms.ToolStripMenuItem menu = (System.Windows.Forms.ToolStripMenuItem)sender;
            if (menu == null || menu.Tag == null) return;

            ((BaseCommand)menu.Tag).OnClick();
        }

        #endregion

    }
}
